<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算器</title>
    <style>
        body {
            font-family: "Arial", sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .calculator {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 320px;
            max-width: 100%;
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            /* 3列 */
            grid-template-rows: repeat(4, 1fr);
            /* 4行，最后一行用于等于按钮 */
            gap: 10px;
        }

        .display {
            background-color: #434343;
            color: white;
            font-size: 2rem;
            text-align: right;
            border-radius: 5px;
            overflow: hidden;
            word-wrap: break-word;
            grid-column: span 3;
            /* 使显示区域跨越3列 */
            padding: 15px;
        }

        .button {
            background-color: #4CAF50;
            color: white;
            font-size: 1.5rem;
            width: 100%;
            height: 60px;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .button:hover {
            background-color: #45a049;
        }

        .button:active {
            background-color: #388e3c;
        }

        .button.clear {
            background-color: #e74c3c;
        }

        .button.clear:hover {
            background-color: #c0392b;
        }

        .button.operator {
            background-color: #f39c12;
        }

        .button.operator:hover {
            background-color: #e67e22;
        }

        .button.equal {
            background-color: #3498db;
            grid-row: span 2;
            /* 使等于按钮跨越两行 */
        }

        .button.equal:hover {
            background-color: #2980b9;
        }

        .button.zero {
            grid-column: span 2;
            /* 让0按钮跨越两列 */
        }
    </style>
</head>

<body>

    <div class="calculator">
        <div class="display" id="display">0</div>

        <!-- 第一行操作符 -->
        <button class="button clear" onclick="clearDisplay()">清除</button>
        <button class="button operator" onclick="appendToDisplay('/')">&divide;</button>
        <button class="button operator" onclick="appendToDisplay('*')">&times;</button>

        <!-- 第二行数字 -->
        <button class="button" onclick="appendToDisplay('7')">7</button>
        <button class="button" onclick="appendToDisplay('8')">8</button>
        <button class="button" onclick="appendToDisplay('9')">9</button>

        <!-- 第三行数字 -->
        <button class="button" onclick="appendToDisplay('4')">4</button>
        <button class="button" onclick="appendToDisplay('5')">5</button>
        <button class="button" onclick="appendToDisplay('6')">6</button>

        <!-- 第四行数字和等于 -->
        <button class="button" onclick="appendToDisplay('1')">1</button>
        <button class="button" onclick="appendToDisplay('2')">2</button>
        <button class="button" onclick="appendToDisplay('3')">3</button>

        <!-- 第五行0和小数点 -->
        <button class="button zero" onclick="appendToDisplay('0')">0</button>
        <button class="button" onclick="appendToDisplay('.')">.</button>

        <button class="button operator" onclick="appendToDisplay('+')">+</button>
        <button class="button operator" onclick="appendToDisplay('-')">-</button>
        <!-- 等于按钮 -->
        <button class="button equal" onclick="calculate()">=</button>
    </div>

    <script>
        const equalEval = str => new Function('return ' + str)();

        let currentInput = '';

        function appendToDisplay(value) {
            if (currentInput === '0' && value !== '.') {
                currentInput = value;
            } else {
                currentInput += value;
            }
            document.getElementById('display').textContent = currentInput;
        }

        function clearDisplay() {
            currentInput = '';
            document.getElementById('display').textContent = '0';
        }

        function calculate() {
            try {
                const result = equalEval(currentInput);
                currentInput = result.toString();
                document.getElementById('display').textContent = currentInput;
            } catch (error) {
                document.getElementById('display').textContent = '错误';
                currentInput = '';
            }
        }
    </script>

</body>

</html>